package ObserverModel3;

import ObserverModel2.WeatherBean;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by Lenovo on 2017/5/16.
 */
public class ConcreteObserver implements Observer {

    /**
     * 观察者
     */
    private String name;
    /**
     * 具体主题
     */
    private ConcreteSubject subject;

    public ConcreteObserver(String name, ConcreteSubject subject) {
        this.name = name;
        this.subject = subject;

        // 绑定观察者
        this.subject.addObserver(this);

        this.update(null, null);
    }

    @Override
    public void update(Observable o, Object arg) {
        String str = null;
        if (arg != null) {
            WeatherBean bean = (WeatherBean) arg;
            str = String.format("name: %s subject: %s", name, bean.toString());
        } else {
            str = String.format("name: %s subject: %s", name, subject.toString());
        }
        System.out.println(str);
    }
}
